<?php

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

class ConfigController extends AppController{
	
	/*****************
	** Variables Generales
	*****************/
	public $name = 'Config';//nombre controlador
	public $uses = array('Config');//modelos empleados
	public $layout = 'admin';//disposición de diseño por defecto

	/*****************
	** Variables Administracion
	*****************/
	public $search_form = false;//mostramos el buscador si o no
	public $sub_menu = array("logs" => "index", "config" => "index");//Subsecciones en submenu
	public $action_menu = null;//Acciones del menu accion

	/*****************
	** Respuestas de controlador a una accion
	*****************/
	public $result_action;//Resultado de la operaciones
	public $msg_result;//Mensaje de la operacion

////////////////////////////////////////////////////////////////////////////
//////*ESTOS METODOS DEBEN ESTAR LIMITADOS A LOS ADMINISTRADORES*//////////
//////////////////////////////////////////////////////////////////////////


	/**
	 * Muestra todas las opciones disponibles del servidor
	 *
	 * @return void
	 */
	public function admin_index(){

		//Seteo las secciones del menu izquierdo
		$this->set('action_menu', $this->action_menu);

		//Seteo las secciones del submenu
		$this->set('sub_menu', $this->sub_menu);

		//Mostramos el buscador
		$this->set('search_form', $this->search_form);	

		//Lenguajes del sistema, reviso la carpeta locale
		$path_locale = ROOT.DS.APP_DIR.DS."locale".DS;
		if (is_dir($path_locale)){
			if ($dir = opendir($path_locale)) {
				while (($file = readdir($dir)) !== false) {
				    if (is_dir($path_locale . $file) && $file!="." && $file!=".."){
				       $list_language[] = $file;
					}
				}
			  closedir($dir);
			}
		}
		$this->set('list_language',$list_language);

		//Temas del sistema, reviso la carpeta themed
		$path_theme = ROOT.DS.APP_DIR.DS."views".DS."themed".DS;
		if (is_dir($path_theme)){
			if ($dir = opendir($path_theme)) {
				while (($file = readdir($dir)) !== false) {
				    if (is_dir($path_theme . $file) && $file!="." && $file!=".."){
				       $list_themes[] = $file;
					}
				}
			  closedir($dir);
			}
		}
		$this->set('list_themes',$list_themes);


		//Datos del registro
		$datos_config = $this->Config->find('all');
		foreach($datos_config as $dato_config){
			$array_config[$dato_config['Config']['data']] = $dato_config['Config']['value']; 
		}
		$this->set('registers', $array_config);
	}



	/**
	 * Actualiza los registros de la BD
	 *
	 * @return void
	 */
	public function admin_add(){
		
		$this->autoRender = false;
		
		if($this->RequestHandler->isAjax() && !empty($this->data)){

			//Aseguro la entrada de datos con sanitize
			$this->data = Sanitize::clean($this->data);

			//Preparo los datos para una correcta entrada en la BD
			foreach ($this->data as $indice => $valor){

				//En este metodo en particular no se valida los datos debido a la estructura de la tabla
				//Salvamos los datos en la BD
				if ($this->Config->setValue($indice,$valor)){
					//Mensaje a lanzar
					$this->result_action = true;
					$this->msg_result = "data_saved_correctly";
				}else{
					$this->result_action = false;
					$this->msg_result = "error_saving";
				}

			}

			####INICIO LOG del sistema
			$data_log = array ( "title" => __('change_main_parameters', true),
								"section" => $this->name,
								"url" => Router::url(array('admin' => true,'controller'=> strtolower($this->name), 'action'=>'index'), true),
								"created" => date("Y-m-d H:i:s")
							);

			$this->Log->addRegister($data_log);
			####FIN LOG

		}else{
			$this->result_action = false;
			$this->msg_result = "form_must_fill";
		}


		//Devuelvo datos del mensaje e identificador (array --> json)
		echo (json_encode(array("result_action" => $this->result_action,
								"msg_result" => __($this->msg_result, true)
								)
						)
			);
	}

}
?>
